package cn.swjtuhc.ss.hljapi.config;

import cn.dev33.satoken.exception.NotLoginException;
import cn.swjtuhc.ss.hljapi.common.Result;
import cn.swjtuhc.ss.hljapi.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: chenximeng
 * @date: 2024/4/9 11:26
 */
@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {


    @ExceptionHandler(BusinessException.class)
    public Result<String> businessExceptionHandler(BusinessException e) {
        log.warn("业务异常", e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public Result<String> notLoginExceptionHandler(NotLoginException e) {
        log.error("未登录或token过期", e);
        return Result.fail(403, "未登录或token过期了, 请重新登陆");
    }

    @ExceptionHandler(NullPointerException.class)
    public Result<String> nullPointerExceptionHandler(NullPointerException e) {
        log.warn("空指针异常", e);
        return Result.fail(500, "空指针了");
    }

    @ExceptionHandler(Exception.class)
    public Result<String> exceptionHandler(Exception e) {
        log.error("系统异常", e);
        return Result.fail(500, "系统异常");
    }

}

